package me.gijspennings.jumpandduck.graphics;

import java.util.Arrays;
import java.util.List;

public class AnimatedSprite extends Sprite {
    
    public static AnimatedSprite player = new AnimatedSprite(SpriteSheet.player, 60, 16, 32, Arrays.asList(0, 1), Arrays.asList(0, 0));
    
    private final int speed; // The amount of updates until the showing sprite switches to the next sprite
    private final int animationLength;
    private int activeSpriteIndex = 0;
    private int updateCount = 0;
    private int[][] sprites;
    
    private AnimatedSprite(SpriteSheet spritesheet, int speed, int width, int height, List<Integer> x, List<Integer> y) {
        super(spritesheet, width, height);
        this.speed = speed;
        animationLength = x.size();
        sprites = new int[animationLength][width * height];
        for (int i = 0; i < animationLength; i++) sprites[i] = loadSprite(x.get(i), y.get(i));
        setActiveSprite();
    }
    
    public void update() {
        updateCount++;
        if (updateCount >= speed) {
            activeSpriteIndex++;
            if (activeSpriteIndex >= animationLength) activeSpriteIndex = 0;
            setActiveSprite();
            updateCount = 0;
        }
    }
    
    private void setActiveSprite() {
        pixels = sprites[activeSpriteIndex];
    }
    
    public int getSpeed() {
        return speed;
    }
    public int getAnimationLength() {
        return animationLength;
    }
    public int getActiveSpriteIndex() {
        return activeSpriteIndex;
    }
    
}
